package com.example.mobileeducationstudentv;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class QuizDisplay extends Activity {

	// variable declarations
	int depth;
	String folderName;
	String subFolderName;
	QuizListAdapter adapter;
	File[] list;
	ListView listView;
	FileDataStorage db;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// initialize database
		db = new FileDataStorage(this);
		// initialize layout
		setContentView(R.layout.display);
		// initialize views and buttons
		listView = (ListView) findViewById(R.id.listview);
		final Button buttonBack = (Button) findViewById(R.id.btnBack);
		final Button buttonClear = (Button) findViewById(R.id.btnClear);
		buttonClear.setVisibility(4);
		buttonBack.setText("Back");
		depth = 0;
		folderName = "";
		subFolderName = "";
		// back button, functions depend on depth
		buttonBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (depth < 1) {
					// goto main menu
					finish();
					depth = 0;
					folderName = "";
				} else if (depth == 2) {
					depth = 1;
					subFolderName = "";
					RefreshList();
				} else {
					// go to folder list
					depth = 0;
					folderName = "";
					subFolderName = "";
					RefreshList();
				}
			}
		});
		// populate listview
		try {
			RefreshList();
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(),
					"Error: Assignment folder not found", Toast.LENGTH_LONG)
					.show();
			finish();
		}
	}

	protected void onResume() {
		super.onResume();
		RefreshList();
	}

	public void RefreshList() {

		final String sdcard = Environment.getExternalStorageDirectory()
				.getAbsolutePath()
				+ "/MobileEducation/Assignments/"
				+ folderName + "/" + subFolderName + "/";
		list = GetFiles(sdcard);
		Boolean[] readFiles = new Boolean[list.length];
		String[] myFiles = new String[0];
		if (list.length > 0) {
			myFiles = new String[list.length];
			for (int i = 0; i < list.length; i++) {
				myFiles[i] = list[i].getName();
				readFiles[i] = (db.isQuizTaken(list[i].getAbsolutePath()) > -1);
				Log.e("Abs Path", "File abs path: " + list[i].getAbsolutePath()
						+ " Fileisread: " + Boolean.toString(readFiles[i]));
			}
		}
		adapter = new QuizListAdapter(this, myFiles, readFiles);

		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			// launch quiz/assignment
			@Override
			public void onItemClick(AdapterView<?> parent, final View view,
					int position, long id) {
				final String item = (String) parent.getItemAtPosition(position);
				File file = new File(sdcard + item);
				Log.e("TEST", "Is dir: " + Boolean.toString(file.isDirectory())
						+ " Depth: " + Integer.toString(depth) + " Name: "
						+ file.getName() + " Path: " + file.getAbsolutePath());
				if ((file.isDirectory()) && (depth == 0)) {
					depth = 1;
					folderName = file.getName();
					RefreshList();
				} else if ((file.isDirectory()) && (depth == 1)) {
					depth = 2;
					subFolderName = file.getName();
					RefreshList();
				} else {
					// open quiz
					int tmpres = db.isQuizTaken(file.getAbsolutePath());
					if (tmpres == -1) {
						Intent assIntent = new Intent(QuizDisplay.this,
								ReadAssignment.class);
						Bundle map = new Bundle();

						map.putString("filename", file.getName());
						map.putString("abspath", file.getAbsolutePath());
						assIntent.putExtra("quizdata", map);

						startActivity(assIntent);
						RefreshList();
					} else {
						Toast.makeText(
								getApplicationContext(),
								"You already completed this assignment.\nYour score was: "
										+ String.valueOf(tmpres) + "% ",
								Toast.LENGTH_LONG).show();
					}
				}

			}
		});
	}

	// get list of files
	public File[] GetFiles(String DirectoryPath) {

		File f = new File(DirectoryPath);
		f.mkdirs();
		File[] files = f.listFiles();
		return files;
	}

}
